#include <libpub.h>
#include <serial.h>
#include <time.h>

extern "C" {
int MODEM_Init();
void MODEM_Close();
}

void FFSerialOutputByte(char c)
{
    if(c == '\n')
        FFSerialOutputByte('\r');

    SerialSelect(1); // Select UART1
    SerialOutputRawByte(c);
    SerialSelect(0); // Select DEBUG UART
}

int FFSerialInputByte(char *c)
{
    int ret;
    SerialSelect(1); // Select UART1
    ret = SerialInputByte(c);
    SerialSelect(0); // Select DEBUG UART
    return ret;
}

#include <time.h>
extern "C" {
void ostdelay(int uSec);
void Codec_CallByReceiver();
}
int _Do_Test_Modem(char *param)
{
    TOUT(1, 1, printf("_Do_Test_Modem (\"%s\")\n", param));

    printf("Enter modem test. Using UART1 to communicate with modem.\n");
    MODEM_Init();
    Codec_CallByReceiver(); // Test 10086 by receiver
    printf("Please input AT COMMAND(q to quit):\n");

    // UART1(MODEM UART) has been initilized in SerialInit

    //1.Get a char from serial
    //2.handle it according to state transision graph
    //3.produce a cmd-string

    char c = '@';

    while(1){
        int ret = Xo_SerialInputByte(&c);
        if (0 == ret) {
            if (c == 'Q') break;
            FFSerialOutputByte(c);
            Xo_SerialOutputByte(c);
        }
        ret = FFSerialInputByte(&c);
        if (1 == ret) {
            Xo_SerialOutputByte(c);
        }
    }

    MODEM_Close();

    return 0;
}
